package com.wave.gate.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.http.HttpUtil;
import com.wave.gate.enums.ConveyerEnum;
import com.wave.gate.enums.RedisKeyEnum;
import com.wave.gate.mqtt.client.MqttSendClient;
import com.wave.gate.util.GuavaCache;
import com.wave.gate.util.SocketClientUtil;
import com.wave.gate.vo.GatePassSetVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.net.Socket;

import static com.wave.gate.util.GuavaCache.DOOR_MODE_CACHE;

/**
 * @author linhui
 * @date 2023/5/20 12:09
 * @version: 1.0
 */
@Slf4j
@RestController
@RequestMapping("/")
public class TestDoorController {
    @Resource
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;

    @Value("${wave.door.security.http}")
    private String http;

    @Value("${mp3.file.path}")
    private String path;

    //A门闸机端口
    public int gateAPort = 502;

    //B门闸机端口
    public int gateBPort = 502;

    /**
     * 00：常开；11：进向自由、出向自由
     */
    private final String doorCommand1 = "000000000006010600410011";

    /**
     * 01：常关；22：进向禁止、出向禁止
     */
    private final String doorCommand2 = "000000000006010600410122";

    /**
     * 01：核验；22：进向禁止、出向禁止
     */
    private final String doorCommand3 = "000000000006010600410122";

    /**
     * 远程操控：1-核验模式、2-常开、3-常闭、4-开门60秒
     */
    @GetMapping(value = "/door/{workMode}")
    public int control(@PathVariable("workMode") int workMode) throws InterruptedException, IOException {

        log.info("给闸机发送命令开始");
        GatePassSetVo gatePassSetVo = GuavaCache.PASSSET_CACHE.getIfPresent("passsetipInfo");
        String gateAIp = gatePassSetVo.getInGateIp();//A门闸机IP
        String gateBIp = gatePassSetVo.getOutGateIp();//B门闸机IP
        Socket doorA = null;
        Socket doorB = null;
        try {
            doorA = new Socket(gateAIp, gateAPort);
            doorB = new Socket(gateBIp, gateBPort);
            String key = RedisKeyEnum.KEY_REMOTE_CONTROLL.getCode();
            DOOR_MODE_CACHE.put(key, workMode);
            if (workMode == 2) {
                String isSuccess_A = SocketClientUtil.sendData(doorA, doorCommand1);
                log.info("A门闸机常开:" + isSuccess_A);
                String isSuccess_B =SocketClientUtil.sendData(doorB, doorCommand1);
                log.info("B门闸机常开:" + isSuccess_B);
            } else if (workMode == 3) {
                String isSuccess_A = SocketClientUtil.sendData(doorA, doorCommand2);
                log.info("A门闸机常闭:" + isSuccess_A);
                String isSuccess_B = SocketClientUtil.sendData(doorB, doorCommand2);
                log.info("B门闸机常闭:" + isSuccess_B);
            } else if (workMode == 4) {
                String isSuccess_A = SocketClientUtil.sendData(doorA, doorCommand1);
                log.info("A门闸机打开60秒开始:" + isSuccess_A);
                String isSuccess_B = SocketClientUtil.sendData(doorB, doorCommand1);
                log.info("B门闸机打开60秒开始:" + isSuccess_B);

                threadPoolTaskExecutor.execute(()->{
                    Socket finalDoorA = null;
                    Socket finalDoorB = null;
                    try {
                        finalDoorA = new Socket(gateAIp, gateAPort);;
                        finalDoorB = new Socket(gateBIp, gateBPort);
                        Thread.sleep(60 * 1000);
                        SocketClientUtil.sendData(finalDoorA, doorCommand2);
                        log.info("A门闸机打开60秒结束" );
                        SocketClientUtil.sendData(finalDoorB, doorCommand2);
                        log.info("B门闸机打开60秒结束");
                        DOOR_MODE_CACHE.put(key, 1);
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }finally {
                        try {
                            if (finalDoorA != null) {
                                finalDoorA.close();
                            }
                            if (finalDoorB != null) {
                                finalDoorB.close();
                            }
                        } catch (IOException e) {
                            throw new RuntimeException(e);
                        }
                    }
                });

            }else {
                log.info("开启核验模式");
                String isSuccess_A = SocketClientUtil.sendData(doorA, doorCommand3);
                log.info("A门闸机开启核验模式:" + isSuccess_A);
                String isSuccess_B = SocketClientUtil.sendData(doorB, doorCommand3);
                log.info("B门闸机开启核验模式:" + isSuccess_B);
            }

        } finally {
            if (doorA != null) {
                doorA.close();
            }
            if (doorB != null) {
                doorB.close();
            }
        }
        log.info("给闸机发送命令开始");
        return 1;
    }

}
